const Database = require('better-sqlite3');
const db = new Database('../db/app.db');

function registerUser(login_name, passwd) {
    try {
        db.prepare('INSERT INTO users (login_name, passwd) VALUES (?, ?)')
            .run(login_name, passwd);
        return { status: 0, msg: 'User registered successfully' };
    } catch (e) {
        return { status: 12, msg: e.message };
    }
}

function authenticateUser(login_name, passwd) {
    try {
        const user = db.prepare('SELECT * FROM users WHERE login_name = ? AND passwd = ?')
            .get(login_name, passwd);
        return user ? { status: 0, msg: 'Authentication successful', user } : { status: 1, msg: 'Invalid credentials' };
    } catch (e) {
        return { status: 12, msg: e.message };
    }
}

module.exports = { registerUser, authenticateUser };